/*
 * Group:PrePool K9-357 
 * 8/2010
 */
package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dto.ApproveDTO;
import dto.WordDTO;

import util.Controller;

/**
 *
 */
public class ApproveDAO {

	/**
	 * @param WordId
	 *            word's Id
	 * @param UserId
	 *            user's Id
	 * @return true if successful,false if fail
	 */
	public static boolean AddApproval(int WordId, int UserId) {
		Connection cnn = null;
		CallableStatement cstat = null;
		ResultSet rs = null;

		try {
			String query = "{call m_approve_insert(?,?)}";
			cnn = Controller.getConnection();
			cstat = cnn.prepareCall(query);
			cstat.setInt(1, WordId);
			cstat.setInt(1, UserId);
			cstat.executeUpdate();

		} catch (Exception ex) {
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (cstat != null) {
					cstat.close();
				}
				if (cnn != null) {
					cnn.close();
				}
			} catch (Exception ex) {
			}
		}

		return true;
	}

	/**
	 * @param m_id
	 *            Approver's Id
	 * @return true if successful,false if fail
	 */
	public static boolean RemoveApproval(int m_id) {
		Connection cnn = null;
		CallableStatement cstat = null;
		ResultSet rs = null;

		try {
			cnn = Controller.getConnection();
			cstat = cnn.prepareCall("{call m_approve_delete(?)}");
			cstat.setInt(1, m_id);
			cstat.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (cstat != null) {
					cstat.close();
				}
				if (cnn != null) {
					cnn.close();
				}
			} catch (Exception ex) {
				return false;
			}
		}
		return true;
	}

	/**
	 * @param m_id
	 *            Approver's Id
	 * @return
	 */
	public static ApproveDTO SelectApproval(int m_id) {
		ApproveDTO record = new ApproveDTO();
		Connection cnn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			cnn = Controller.getConnection();
			pstmt = cnn
					.prepareStatement("select * from m_approve where m_id = ?");
			pstmt.setInt(1, m_id);
			rs = pstmt.executeQuery();
			
			while (rs.next()) {
				record.setM_id(rs.getInt("m_id"));
				record.setM_userID(rs.getInt("m_userID"));
				record.setM_wordID(rs.getInt("m_wordID"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return record;
	}

}
